//
//  DeviceSelectUserViewController.m
//  Rail
//
//  Created by hychen on 2018/7/25.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "DeviceSelectUserViewController.h"
#import "EVNCustomSearchBar.h"
#import "GetDeviceInfoNormalTableViewCell.h"

@interface DeviceSelectUserViewController ()<EVNCustomSearchBarDelegate>
@property (nonatomic, copy) NSString *searchText;
@property (nonatomic, strong) EVNCustomSearchBar *searchView;
@end

@implementation DeviceSelectUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"选择用户"];
    
    [self setupSearchView];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.searchView.mas_bottom);
    }];
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupSearchView {
    self.searchView = [[EVNCustomSearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44)];
    self.searchView.backgroundColor = Color_NavBgColor;
    self.searchView.iconAlign = EVNCustomSearchBarIconAlignCenter;
    self.searchView.iconImage = [UIImage imageNamed:@"EVNCustomSearchBar.bundle/searchImageBlack"];
    self.searchView.placeholder = @"搜索用户";
    self.searchView.delegate = self;
    self.searchView.isHiddenCancelButton = NO;
    [self.view addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(45);
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DeviceUserInfoModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    GetDeviceInfoNormalTableViewCell *cell = [GetDeviceInfoNormalTableViewCell cellWithTableView:tableView class:[GetDeviceInfoNormalTableViewCell class]];
    cell.userModel = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 75;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DeviceUserInfoModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    if (!model) {
        return;
    }
    if (self.BlockSelectUser) {
        self.BlockSelectUser(model);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)refreshData {
    self.currentPage = 1;
    BMWeakSelf
    [HttpRequest loadDevicePickUserListDataWithKeyword:self.searchText pageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
        if (object.count > 0) {
            weakSelf.dataArray = [NSMutableArray arrayWithArray:object];
            weakSelf.currentPage++;
            weakSelf.isOpenFooter = YES;
            [weakSelf.tableView.mj_footer resetNoMoreData];
        }
        else {
            weakSelf.dataArray = [NSMutableArray array];
            weakSelf.isOpenFooter = NO;
        }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

- (void)loadMoreData {
    BMWeakSelf
    [HttpRequest loadDevicePickUserListDataWithKeyword:self.searchText pageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
        if (object.count>0) {
            weakSelf.currentPage++;
            [weakSelf.dataArray addObjectsFromArray:object];
        }
        else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_footer endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

- (void)searchBarTextDidEndEditing:(EVNCustomSearchBar *)searchBar {
    self.searchText = searchBar.text;
    [self.tableView.mj_header beginRefreshing];
}

- (void)searchBarSearchButtonClicked:(EVNCustomSearchBar *)searchBar {
    self.searchText = searchBar.text;
    [self.tableView.mj_header beginRefreshing];
}

- (void)searchBar:(EVNCustomSearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ([searchText isEqualToString:@""]) {
        self.searchText = searchText;
        [self refreshData];
    }
}

- (void)searchBarCancelButtonClicked:(EVNCustomSearchBar *)searchBar {
    [self.searchView resignFirstResponder];
}
@end
